/*
 * Copyright (c) GoKeep by gokeep.org. 2022-2023. All rights reserved
 */
package org.gokeep.common.exception;

import lombok.Data;
import org.gokeep.common.util.JsonUtil;
import org.springframework.http.HttpStatus;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xuning
 */
@Data
public class GoKeepException extends RuntimeException {
    private final int code;
    private final String msg;
    private final String tip;


    public GoKeepException(String msg) {
        this(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, "服务器内部错误");
    }

    public GoKeepException(String msg, String tip) {
        this(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, tip);
    }

    public GoKeepException(int code, String msg, String tip) {
        super(msg);
        this.code = code;
        this.msg = msg;
        this.tip = tip;
    }

    public GoKeepException(HttpStatus httpStatus, String msg, String tip) {
        super(msg);
        this.code = httpStatus.value();
        this.msg = msg;
        this.tip = tip;
    }

    public byte[] toByteArray() {
        Map<String, Object> object = new HashMap<>();
        object.put("code", this.code);
        object.put("msg", this.msg);
        object.put("tip", this.tip);
        return JsonUtil.toJsonBytes(object);
    }
}
